package com.lxj.service.impl;

import com.lxj.domain.GroupMember;
import com.lxj.domain.User;
import com.lxj.mapper.UserMapper;
import com.lxj.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.Jedis;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public List<User> findAll(User user) {
        return userMapper.findAll(user);
    }

    @Override
    public int delete(Integer[] id) {
        return userMapper.delete(id);
    }

    @Override
    public int edit(User user) {
        return userMapper.edit(user);
    }

    @Override
    public User findById(Integer id) {
        return userMapper.findById(id);
    }

    @Override
    public int getCount() {
        return userMapper.getCount();
    }

    @Override
    public List<User> findNoGroupUser() {
        return userMapper.findNoGroupUser();
    }

    @Override
    public int selectUserId(String s) {
        return userMapper.selectUserId(s);
    }

    @Override
    public int deleteGroup(Integer id) {
        return userMapper.deleteGroup(id);
    }

    @Override
    public int updateGroup(GroupMember groupMember) {
        return userMapper.updateGroup(groupMember);
    }

    @Override
    public Map<String, Object> login(User user) {
        Map<String, Object> map = new HashMap<>();

        //先判断用户名是否存在
        int a = userMapper.judgeUsername(user);
        if (a == 1) {
            //如果存在，判断密码是否正确
            int b = userMapper.judgePassword(user);
            if (b == 1) {
                //先判断账号是否封禁
                User user1 = userMapper.judgeState(user);
                if (user1.getState() == 1) {
                    //用户名和密码都正确，则把username存在缓存，key为uuid，把uuid和1返回
                    Jedis jedis = new Jedis();
                    String uuid = UUID.randomUUID().toString();
                    jedis.set(uuid, user.getUsername());
                    jedis.expire(uuid,60*60*24);
                    map.put("usernameKey", uuid);
                    map.put("result", 1);
                } else {
                    //账户已封禁！
                    map.put("result", 200);
                }
            } else {
                // 返回3，密码错误
                map.put("result", 3);
            }
        } else {
            //返回2 用户名不存在
            map.put("result", 2);
        }
        return map;
    }

    @Override
    public String getName(String token) {
        Jedis jedis = new Jedis();
        return  jedis.get(token);
    }

    @Override
    public User findByUserName(User user) {
        return userMapper.findByUserName(user);
    }

    @Override
    @Transactional
    public int editPassword(User user) {
        int result = inspect(user);
        if (result==1){
            return userMapper.editPassword(user);
        }else {
            return 2;
        }

    }

    @Override
    public int inspect(User user) {
        return userMapper.inspect(user);
    }
}
